﻿using System;
using System.Linq;
using System.Web.Mvc;
using Shell.DomainDot;
using Framework.Poco;
using Framework.Domain;

namespace Framework.Mvc.Controllers
{
    public class AgencyPictureCategoryController : Controller
    {
        private IDomainDotFactory factory;

        public AgencyPictureCategoryController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 提交图片分类创建
        /// </summary>
        public ActionResult Create(Agency currentAgency, PictureCategory instance)
        {
            if (currentAgency != null)
            {
                if (String.IsNullOrEmpty(instance.Name))
                {
                    this.ModelState.AddModelError("instance.Name", "分类名称不能为空!");
                    return View(instance);
                }
                var context = factory.Create<PictureCategoryContext>(currentAgency.Id);
                context.Create(instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 删除经销商图片分类
        /// </summary>
        public ActionResult Delete(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var pictureContext = factory.Create<PictureContext>(identity);
                if (pictureContext.GetAll().Count() == 0)
                {
                    var pictureCategoryContext = factory.Create<PictureCategoryContext>(currentAgency.Id);
                    pictureCategoryContext.Delete(identity);
                    return this.RedirectToAction("List");
                }
                return this.RedirectToAction("List", "Picture", new { identity = identity });
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改图片分类
        /// </summary>
        [HttpGet]
        public ActionResult Edit(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var model = new AgencyPictureCategoryEditViewModel();
                var context = factory.Create<PictureCategoryContext>(currentAgency.Id);
                model.Instance = context.GetPictureCategory(identity);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改图片分类
        /// </summary>
        [HttpPost]
        public ActionResult Edit(Agency currentAgency, AgencyPictureCategoryEditViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<PictureCategoryContext>(currentAgency.Id);
                context.Edit(model.Instance);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 图片分类列表
        /// </summary>
        public ActionResult List(Agency currentAgency, int currentPage, int pageSize)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<PictureCategoryContext>(currentAgency.Id);
                var list = context.Select(currentPage, pageSize);
                var model = new AgencyPictureCategoryListViewModel();
                model.Total = context.GetAllAgencyPictureCategory().Count();
                model.List = list;
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }
    }
}
